package Buffer;

import java.io.*;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

public class TreeSetToFileDemo {
    public static void main(String[] args) throws IOException {

        TreeSet<SDemo> treeSet = new TreeSet<>(new Comparator<SDemo>() {
            @Override
            public int compare(SDemo o1, SDemo o2) {

                int num = o2.getSum() - o1.getSum();
                int num2 = num==0?o1.getCCourse() - o2.getCCourse():num;
                int num3 = num2==0?o1.getMCourse() - o2.getMCourse():num2;
                int num4 = num3 == 0 ? o1.getName().compareTo(o2.getName()) : num3;
                return num4;
            }
        });

        //键盘录入学生数据
        for (int i = 0; i < 5; i++) {
            Scanner sc = new Scanner(System.in);
            System.out.println("亲输入第"+(i+1) + "个学生信息");
            String s = sc.nextLine();
            String[] split = s.split(",");

            SDemo s1 = new SDemo(split[0],
                    Integer.parseInt(split[1]),
                    Integer.parseInt(split[2]),
                    Integer.parseInt(split[3]));
            treeSet.add(s1);
        }
        //输出
        BufferedWriter bw = new BufferedWriter(new FileWriter("funCode\\fos.txt"));
        for (SDemo s : treeSet
        ) {
            String s1 = s.getName() + "," + s.getMCourse()
                                    + "," + s.getCCourse()
                                    + "," + s.getECourse()
                                    + "," + s.getSum();
            bw.write(s1);
            bw.newLine();
            bw.flush();
        }
        bw.close();
    }
}
